<template>
	<view class="content">
		<view class="tipsView">
			<view class="tipLine"></view>
			<text>当前数据上传情况</text>
		</view>
		<view class="recordItem" style="width: 700rpx;">
			<view class="weekTitle">
				<view class="weekNumber" style="font-size: 1rem;">当前周</view>
				<view class="weekTitle_date"> {{currentStart.slice(0,10).replace(/-/g,"/")}} </view>
			</view>
			<view class="recordItemB">
				<view class="starView" v-for="(item,index) in currentRecord" :key="index">
					<image v-if="item" :src="staticUrl + '/image/mine/icon_starY.png'" mode="widthFix"></image>
					<image v-else :src="staticUrl + '/image/mine/icon_starG.png'" mode="widthFix"></image>
				</view>
				<view class="finishPrcent">({{currentDays}}/7)</view>
			</view>
			<view v-if="currentDays > 4" class="recordTip tipColorY"></view>
			<view v-else class="recordTip tipColorG"></view>
			<view class="recordTipText">{{currentDays > 4?'完成':'未完成'}}</view>
		</view>
		
		<view class="tipsView">
			<view class="tipLine"></view>
			<text>历史数据上传情况</text>
		</view>
		<view v-if="!hasHistory" class="content" style="width: 500rpx;height: 300rpx;">
			<text>暂无历史上传记录</text>
		</view>
		<view v-else class="recordListView content">
			<view class="recordItem" v-for="(item,index) in recordList" :key="index">
				<view class="weekTitle">
					<view class="weekNumber">{{index + 1}} 周</view>
					<view class="weekTitle_date"> {{item.start.slice(0,10).replace(/-/g,"/")}} </view>
				</view>
				<view class="recordItemB">
					<view class="starView">
						<image v-if="item.first" :src="staticUrl + '/image/mine/icon_starY.png'" mode="widthFix"></image>
						<image v-else :src="staticUrl + '/image/mine/icon_starG.png'" mode="widthFix"></image>
						<image v-if="item.second" :src="staticUrl + '/image/mine/icon_starY.png'" mode="widthFix"></image>
						<image v-else :src="staticUrl + '/image/mine/icon_starG.png'" mode="widthFix"></image>
						<image v-if="item.third" :src="staticUrl + '/image/mine/icon_starY.png'" mode="widthFix"></image>
						<image v-else :src="staticUrl + '/image/mine/icon_starG.png'" mode="widthFix"></image>
						<image v-if="item.fourth" :src="staticUrl + '/image/mine/icon_starY.png'" mode="widthFix"></image>
						<image v-else :src="staticUrl + '/image/mine/icon_starG.png'" mode="widthFix"></image>
						<image v-if="item.fifth" :src="staticUrl + '/image/mine/icon_starY.png'" mode="widthFix"></image>
						<image v-else :src="staticUrl + '/image/mine/icon_starG.png'" mode="widthFix"></image>
						<image v-if="item.sixth" :src="staticUrl + '/image/mine/icon_starY.png'" mode="widthFix"></image>
						<image v-else :src="staticUrl + '/image/mine/icon_starG.png'" mode="widthFix"></image>
						<image v-if="item.seventh" :src="staticUrl + '/image/mine/icon_starY.png'" mode="widthFix"></image>
						<image v-else :src="staticUrl + '/image/mine/icon_starG.png'" mode="widthFix"></image>
					</view>
					<view class="finishPrcent">({{signInDays[index]}}/7)</view>
				</view>
				<view v-if="item.isCompleted" class="recordTip tipColorY"></view>
				<view v-else class="recordTip tipColorG"></view>
				<view class="recordTipText">{{item.isCompleted?'完成':'未完成'}}</view>
			</view>
		</view>
	</view>
</template>

<script>
	import * as api from '@/api/login';
	export default {
		data() {
			return {
				staticUrl: this.$config.StaticUrl,
				recordList: [],
				signInDays: 0,
				currentRecord: [],
				currentDays: 0,
				currentStart: '',
				hasHistory: false,
			}
		},
		onLoad(option) {
			console.log('option: ', option.serviceId);
			this.userId = this.$store.state.userInfo.currentUser.userId;
			this.getServiceWeekRecord(option.serviceId)
			this.getServiceWeekHistoryRecord(option.serviceId)
		},
		methods: {
			// 获取当前周的完成情况
			getServiceWeekRecord(serviceId){
				let that = this;
				let data = {
					userId: this.userId,
					params: serviceId
				};
				api.getServiceWeekRecord(data).then(function(res) {
					console.log('getServiceWeekRecord: ', res);
					if(res.data.success) {
						that.currentStart = res.data.data.record.start
						for(let key in res.data.data.record) {
							if(key !== 'serviceId' && key !== 'start') {
								that.currentRecord.push(res.data.data.record[key])
								if(res.data.data.record[key]) that.currentDays++
							}
						}
					}
				}).catch(function(error) {
					console.log('getPurseInfo fail', error);
				});
			},
			//获取历史签到记录
			getServiceWeekHistoryRecord(serviceId) {
				let that = this;
				let data = {
					userId: this.userId,
					params: serviceId
				};
				api.getServiceWeekHistoryRecord(data).then(function(res) {
					console.log('res: ', res);
					if(res.data.success) {
						if(res.data.data.recordList.length == 0){
							that.hasHistory = false
							return
						}
						that.hasHistory = true
						that.recordList = res.data.data.recordList
						let _signInNumber = []
						res.data.data.recordList.forEach((item, index) => {
							let count = 0
							for (let key in item) {
								if (key !== 'serviceId' && key !== 'start' && key !== 'id' && key !==
									'isCompleted') {
									if (item[key]) count++
								}
							}
							_signInNumber.push(count)
						})
						that.signInDays = _signInNumber
					}
				}).catch(function(error) {
					console.log('getPurseInfo fail', error);
				});
			},
		}
	}
</script>

<style>
	page {
		background-color: #f5f5f5;
	}

	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
	.tipsView {
		margin-top: 20rpx;
		width: 700rpx;
		display: flex;
		align-items: center;
	}
	.tipLine {
		width: 8rpx;
		height: 50rpx;
		background-color: #0659c7;
		border-radius: 50rpx;
	}
	.tipsView text {
		margin-left: 20rpx;
	}
	.recordListView {
		margin-top: 20rpx;
		width: 700rpx;
	}

	.recordItem {
		position: relative;
		margin-top: 15rpx;
		width: 100%;
		height: 150rpx;
		background-color: #FFFFFF;
		border-radius: 15px;
		display: flex;
		flex-direction: column;
		align-items: flex-start;
		justify-content: center;
	}

	.recordTip {
		position: absolute;
		top: 0;
		right: 0;
		border: 40rpx solid;
		border-radius: 0 15px 0 0;
	}

	.tipColorY {
		border-color: #f19149 #f19149 transparent transparent;
	}

	.tipColorG {
		border-color: #8c8c8c #8c8c8c transparent transparent;
	}

	.recordTipText {
		position: absolute;
		top: 13rpx;
		right: 10rpx;
		font-size: 20rpx;
		color: #FFFFFF;
		transform: rotate(45deg);
	}

	.weekTitle {
		margin-left: 50rpx;
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: flex-start;
	}

	.weekNumber {
		font-size: 1.2rem;
		color: #000000;
		font-weight: bold;
		/* font-family: aliDig; */
	}

	.weekTitle_date {
		margin-left: 30rpx;
		color: #666666;
	}

	.recordItemB {
		margin-top: 10rpx;
		margin-left: 50rpx;
		width: 600rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.starView {
		width: 450rpx;
		display: flex;
		align-items: center;
		justify-content: space-around;
	}

	.starView image {
		width: 40rpx;
	}

	.finishPrcent {
		color: #666666;
	}
</style>